import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../../module_common/widgets/image/public_image.dart';
import '../model/integral_model.dart';

class progressWidget extends StatefulWidget {
  final String rules;
  const progressWidget({super.key, required this.rules});

  @override
  State<progressWidget> createState() => _progressWidgetState();
}

class _progressWidgetState extends State<progressWidget> {
   List<IntegralRule> get _rules =>  (jsonDecode(widget.rules) as List<dynamic>)
       .map((dynamic rule) => IntegralRule.fromJson(rule))
       .toList();
   int get _progressValue {
     List<IntegralRule> filteredRules = _rules.where((rule) {
       return rule.status == 1 || rule.status == 3;
     }).toList();
     return filteredRules.length;
   }
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if (_rules.isEmpty) {
      return Container(height: 30.w);
    }
    //实际的进度实图
    return Stack(
      children: [
        Container(
          height: 3.w,
          width: 22.w,
          color: _progressValue > 0
              ? Color.fromRGBO(134, 103, 254, 1)
              : Color.fromRGBO(224, 224, 224, 1),
          margin: EdgeInsets.fromLTRB(0, 7, 0, 7),
        ),
        Padding(
          padding: EdgeInsets.fromLTRB(22.w, 0, 0, 0),
          child: Row(
            children: [
              Expanded(
                flex: (_progressValue - 1),
                child: Container(
                  height: 3.w,
                  color: Color.fromRGBO(134, 103, 254, 1),
                  margin: EdgeInsets.fromLTRB(0, 7, 0, 7),
                ),
              ),
              Expanded(
                flex: (_rules.length - 1) - (_progressValue - 1),
                child: Container(
                  height: 3.w,
                  color: Color.fromRGBO(224, 224, 224, 1),
                  margin: EdgeInsets.fromLTRB(0, 7, 0, 7),
                ),
              )
            ],
          ),
        ),
        Padding(
          padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
          child: Row(
            mainAxisSize: MainAxisSize.max, // 让 Row 占满可用空间
            mainAxisAlignment: MainAxisAlignment.spaceBetween, // 子组件之间的空间分配
            children: getChildren(),
          ),
        )
      ],
    );
  }

  getprogressChildren() {
    var children = _rules.map((e) {
      return Container(
        height: 3.w,
        width: 55,
        color: Color.fromRGBO(134, 103, 254, 1),
        margin: EdgeInsets.fromLTRB(0, 7, 0, 7),
      );
    }).toList();

    children.insert(
        0,
        Container(
          width: 1,
          height: 10,
          color: Colors.black,
        ));

    return children;
  }

   List<Widget> getChildren() {
    List<Widget> children = [];
    for(var i =0;i<_rules.length;i++){
      final e=_rules[i];
      if(i==0){
        children.add(Container(child: progressItemWidget(e),width: 28.w,));
        continue;
      }
      children.add(Expanded(child: Container(child: progressItemWidget(e)),flex: 1,));
    }
    return children;
  }

  Widget progressItemWidget(IntegralRule rule) {
    return Container(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          // Container(
          //   width: 20.w,
          //   height: 70.w,
          //   color: const Color.fromRGBO(33, 150, 243, 0.3),
          // )
          PublicImage(
            image: statusImageName(rule.status),
            width: 16.w,
            height: 16.w,
          ),
          SizedBox(height: 1.w),

          ConstrainedBox(
            constraints: BoxConstraints(maxWidth: 50.w), // 设置最大宽度
            child: Text(
              rule.value,
              maxLines: 1,
              textAlign: TextAlign.right,
              overflow: TextOverflow.visible, // 设置超出时显示省略号
              style: TextStyle(
                  fontSize: 10.sp,
                  color: Color.fromRGBO(51, 51, 51, 1),
                  fontWeight: FontWeight.w400),
            ),
          ), // 设置最大宽度

          SizedBox(height: 1.w),
          Text(
            '+${rule.integral}KC',
            maxLines: 1,
            textAlign: TextAlign.right,
            overflow: TextOverflow.visible, // 设置超出时显示省略号
            style: TextStyle(
                fontSize: 8.sp,
                color: Color.fromRGBO(134, 103, 254, 1),
                fontWeight: FontWeight.w400),
          ),
        ],
      ),
    );
  }

  String statusImageName(int status) {
    switch (status) {
      case 0:
        return 'assets/images/integral/gift_grey.png';
      case 1:
        return 'assets/images/integral/gift.png';
      case 3:
        return 'assets/images/integral/selected.png';
      default:
        return '';
    }
  }
}
